package com.example.myprojectoffeixue;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.test.demo.DeviceListViewActivity;

import org.apache.http.entity.StringEntity;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;

public class HouseDeviceActivity extends Activity {
     Dialog dialog;
     MyApplication m;
     ListView lv_device;
    adapter_HouseDevice ld;




    Handler hand = new Handler() {

        public void handleMessage(android.os.Message msg) {
            if (msg.what == 111) {
                    ld.notifyDataSetChanged();
            }else if (msg.what == 60) {
              for(int i=0;i<m.hu.devices.size();i++) {
                 if("010E".equals(m.hu.devices.get(i).deviceId)){
                  HttpConnection.toastLog("检查到有人移动", getBaseContext());}
              }
            }else if(msg.what==10){
                ld.notifyDataSetChanged();
            }
        };
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_house_device);
    }

    @Override
    protected void onStart() {
        info();
        super.onStart();
    }

    public void info(){
    ImageView add_device= (ImageView) findViewById(R.id.add_device);
    add_device.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(m.Nodevice_house!=null){
            Intent it=new Intent(getBaseContext(),ChoiceSbActivity.class);
            it.putExtra("update",true);
            startActivity(it);}else{
                HttpConnection.showToast(getBaseContext(),"不存在未分配的设备");
            }
        }
    });



     m= (MyApplication) getApplication();
     m.hand=hand;
     TextView tv= (TextView) findViewById(R.id.name);
     tv.setText(m.hu.name);
     lv_device= (ListView) findViewById(R.id.lv_device);
     setadapter();
    lv_device.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        private Intent it;

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


           if ("0020".equals(m.hu.devices.get(position).deviceId)) {

                it=new Intent(getBaseContext(),MyYaoKongActivity.class);
                m.ieee_ykq=m.hu.devices.get(position).ieee;
                m.port_ykq=m.hu.devices.get(position).port;
                m.mac_ykq=m.hu.devices.get(position).mac;
                startActivity(it);
            } else if ("010F".equals(m.hu.devices.get(position).deviceId)) {
                m.t=new Extra();
                m.t.powerStatus="00";
                m.t.mode="01";
                m.t.speed="00";
                m.t.temperature="16";
                m.t.roomTemperature="25";
                m.t.timerStatus="00";
                m.t.timerHour="00";
                m.t.timerMin="00";
                m.t.timerType="01";
                it = new Intent(getBaseContext(), KongTiaoActivity.class);
                it.putExtra("ieee", m.hu.devices.get(position).ieee);
                it.putExtra("port", m.hu.devices.get(position).port);
                it.putExtra("mac", m.hu.devices.get(position).mac);
                startActivity(it);

            }
        }
    });


       lv_device.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
           @Override
           public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {

                dialog = new Dialog(HouseDeviceActivity.this,R.style.dialog);
                dialog.setContentView(R.layout.yaokongqidialog);
                TextView tv = (TextView) dialog.findViewById(R.id.restudy);
                tv.setVisibility(View.GONE);
                TextView delete = (TextView) dialog.findViewById(R.id.delete);
                TextView iv1 = (TextView) dialog.findViewById(R.id.iv1_mydialog);
                dialog.show();
                delete.setOnClickListener(new View.OnClickListener() {
                   @Override
                   public void onClick(View v) {
                       dialog.dismiss();
                       AlertDialog.Builder builder = new AlertDialog.Builder(HouseDeviceActivity.this);

                       builder.setMessage("确认删除吗?");

                       builder.setTitle("提示");

                       builder.setPositiveButton("确认", new Dialog.OnClickListener() {

                           @Override

                           public void onClick(DialogInterface dialog, int which) {
                               UpdateRoom(position);
                           }
                       });
                       builder.setNegativeButton("取消", new Dialog.OnClickListener() {

                           @Override

                           public void onClick(DialogInterface dialog, int which) {
                               dialog.dismiss();
                           }
                       });
                       builder.create().show();
                   }
               });
               return true;
           }
       });









}





public void UpdateRoom(final int position){

    HouseUtil h = new HouseUtil();
    h.userId=m.userId;
    h.name=m.hu.name;
    h.id=m.hu.id;
    h.deviceList=new ArrayList<SheBeiClass>();
    h.deviceList.addAll(m.hu.devices);
    h.deviceList.remove(position);
    String j = JSON.toJSONString(h);
    Log.d("cmd444",j);
    j = AESUtil.encrypt(j);
    HttpUtils httpUtils = new HttpUtils();
    RequestParams params = new RequestParams();
    params.setHeader("Content-Type", "application/json");
    try {
        params.setBodyEntity(new StringEntity(j, "utf-8"));
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    httpUtils.send(HttpRequest.HttpMethod.POST, HttpConnection.updateLabel, params,

            new RequestCallBack<String>() {

                @Override
                public void onFailure(HttpException arg0, String arg1) {
                    ToastUtils.showToast(getBaseContext(),"网络异常");
                }

                @Override
                public void onSuccess(ResponseInfo<String> arg0) {
                    if (arg0.result != null && !("".equals(arg0.result))) {
                        arg0.result = AESUtil.decrypt(arg0.result);
                        Log.d("result5555",arg0.result);
                        JSONObject jb = JSONObject.parseObject(arg0.result);
                        String msg = jb.getString("msg");
                        if ("成功".equals(msg)) {
                            m.hu.devices.remove(position);
                            ld.notifyDataSetChanged();
                        }else{
                            HttpConnection.toastLog("服务器错误", getBaseContext());
                        }
                    } else {
                        HttpConnection.toastLog("服务器错误", getBaseContext());
                    }
                }

            });






















}











    public void setadapter() {
        for (int i = 0; i < m.hu.devices.size(); i++) {

            if ("0100".equals(m.hu.devices.get(i).deviceId)) {
                // 开关
                if (m.hu.devices.get(i).deviceName == null) {
                    m.hu.devices.get(i).deviceName = "智能开关";
                }

            } else if ("010B".equals(m.hu.devices.get(i).deviceId) || "010A".equals(m.hu.devices.get(i).deviceId)) {
                // 门锁
                if (m.hu.devices.get(i).deviceName == null) {
                    m.hu.devices.get(i).deviceName = "智能门锁";
                }
                if (m.hu.devices.get(i).status == null) {
                    m.hu.devices.get(i).status = "00";
                }

            } else if ("010D".equals(m.hu.devices.get(i).deviceId)) {
                // 门磁
                if (m.hu.devices.get(i).deviceName == null) {
                    m.hu.devices.get(i).deviceName = "智能门磁";
                }

            } else if ("010E".equals(m.hu.devices.get(i).deviceId)) {
                // 红外
                if (m.hu.devices.get(i).deviceName == null) {
                    m.hu.devices.get(i).deviceName = "红外人体感应";
                }


            } else if ("010C".equals(m.hu.devices.get(i).deviceId)) {
                // 取电开关
                if (m.hu.devices.get(i).deviceName == null) {
                    m.hu.devices.get(i).deviceName = "取电开关";
                }

            } else if ("0002".equals(m.hu.devices.get(i).deviceId)) {

                // 窗帘
                if (m.hu.devices.get(i).deviceName == null) {
                    m.hu.devices.get(i).deviceName = "智能窗帘";
                }

            } else if ("010F".equals(m.hu.devices.get(i).deviceId)) {
                if (m.hu.devices.get(i).deviceName == null) {
                    m.hu.devices.get(i).deviceName = "中央空调";
                }


            } else if ("0020".equals(m.hu.devices.get(i).deviceId)) {
                if (m.hu.devices.get(i).deviceName == null) {
                    m.hu.devices.get(i).deviceName = "红外转发器";
                }


            } else if ("0009".equals(m.hu.devices.get(i).deviceId)) {
                if (m.hu.devices.get(i).deviceName == null) {
                    m.hu.devices.get(i).deviceName = "智能插座";
                }
                }}

        ld=new adapter_HouseDevice(HouseDeviceActivity.this);
        lv_device.setAdapter(ld);

    }























            }



